package net.wanglu.www.zzz.rule.artcl;

import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class Country extends _ArticleRule {

	public Pattern Country = Pattern
			.compile("\\bRussia\\b|\\bAntarctica\\b|\\bCanada\\b|\\bChina\\b|\\bUnited States\\b|\\bBrazil\\b|\\bAustralia\\b|\\bIndia\\b|\\bArgentina\\b|\\bKazakhstan\\b|\\bAlgeria\\b|\\bGreenland\\b|\\bDenmark\\b|\\bSaudi Arabia\\b|\\bMexico\\b|\\bIndonesia\\b|\\bSudan\\b|\\bLibya\\b|\\bIran\\b|\\bMongolia\\b|\\bPeru\\b|\\bChad\\b|\\bNiger\\b|\\bAngola\\b|\\bMali\\b|\\bSouth Africa\\b|\\bColombia\\b|\\bEthiopia\\b|\\bBolivia\\b|\\bMauritania\\b|\\bEgypt\\b|\\bTanzania\\b|\\bNigeria\\b|\\bVenezuela\\b|\\bPakistan\\b|\\bNamibia\\b|\\bMozambique\\b|\\bTurkey\\b|\\bChile\\b|\\bZambia\\b|\\bBurma\\b|\\bAfghanistan\\b|\\bFrance\\b|\\bSomalia\\b|\\bCentral African Republic\\b|\\bSouth Sudan\\b|\\bUkraine\\b|\\bMadagascar\\b|\\bBotswana\\b|\\bKenya\\b|\\bYemen\\b|\\bThailand\\b|\\bSpain\\b|\\bTurkmenistan\\b|\\bCameroon\\b|\\bPapua New Guinea\\b|\\bSweden\\b|\\bUzbekistan\\b|\\bMorocco\\b|\\bIraq\\b|\\bParaguay\\b|\\bZimbabwe\\b|\\bJapan\\b|\\bGermany\\b|\\bFinland\\b|\\bVietnam\\b|\\bMalaysia\\b|\\bNorway\\b|\\bPoland\\b|\\bOman\\b|\\bItaly\\b|\\bBurkina Faso\\b|\\bNew Zealand\\b|\\bGabon\\b|\\bWestern Sahara\\b|\\bEcuador\\b|\\bGuinea\\b|\\bUnited Kingdom\\b|\\bUganda\\b|\\bGhana\\b|\\bRomania\\b|\\bLaos\\b|\\bGuyana\\b|\\bBelarus\\b|\\bKyrgyzstan\\b|\\bSenegal\\b|\\bSyria\\b|\\bCambodia\\b|\\bUruguay\\b|\\bSuriname\\b|\\bTunisia\\b|\\bBangladesh\\b|\\bNepal\\b|\\bTajikistan\\b|\\bSomaliland\\b|\\bGreece\\b|\\bNicaragua\\b|\\bMalawi\\b|\\bEritrea\\b|\\bBenin\\b|\\bHonduras\\b|\\bLiberia\\b|\\bBulgaria\\b|\\bCuba\\b|\\bGuatemala\\b|\\bIceland\\b|\\bHungary\\b|\\bPortugal\\b|\\bJordan\\b|\\bSerbia\\b|\\bAzerbaijan\\b|\\bAustria\\b|\\bUnited Arab Emirates\\b|\\bCzech Republic\\b|\\bPanama\\b|\\bSierra Leone\\b|\\bIreland\\b|\\bGeorgia\\b|\\bSri Lanka\\b|\\bLithuania\\b|\\bLatvia\\b|\\bSvalbard\\b|\\bTogo\\b|\\bCroatia\\b|\\bBosnia and Herzegovina\\b|\\bCosta Rica\\b|\\bSlovakia\\b|\\bDominican Republic\\b|\\bEstonia\\b|\\bSwitzerland\\b|\\bBhutan\\b|\\bTaiwan\\b|\\bGuinea-Bissau\\b|\\bMoldova\\b|\\bBelgium\\b|\\bLesotho\\b|\\bArmenia\\b|\\bSolomon Islands\\b|\\bAlbania\\b|\\bEquatorial Guinea\\b|\\bBurundi\\b|\\bHaiti\\b|\\bRwanda\\b|\\bMacedonia\\b|\\bDjibouti\\b|\\bBelize\\b|\\bEl Salvador\\b|\\bIsrael\\b|\\bSlovenia\\b|\\bNew Caledonia\\b|\\bFiji\\b|\\bKuwait\\b|\\bSwaziland\\b|\\bEast Timor\\b|\\bMontenegro\\b|\\bVanuatu\\b|\\bFalkland Islands\\b|\\bQatar\\b|\\bNagorno-Karabakh\\b|\\bJamaica\\b|\\bKosovo\\b|\\bLebanon\\b|\\bCyprus\\b|\\bPuerto Rico\\b|\\bAbkhazia\\b|\\bFrench Southern and Antarctic Lands\\b|\\bUnited States Pacific Island Wildlife Refuges\\b|\\bState of Palestine\\b|\\bBrunei\\b|\\bTrinidad and Tobago\\b|\\bTransnistria\\b|\\bCape Verde\\b|\\bFrench Polynesia\\b|\\bSouth Georgia and the South Sandwich Islands\\b|\\bSouth Ossetia\\b|\\bNorthern Cyprus\\b|\\bSamoa\\b|\\bLuxembourg\\b|\\bMauritius\\b|\\bComoros\\b|\\bFaroe Islands\\b|\\bHong Kong\\b|\\bTurks and Caicos Islands\\b|\\bKiribati\\b|\\bBahrain\\b|\\bDominica\\b|\\bTonga\\b|\\bSingapore\\b|\\bSaint Lucia\\b|\\bIsle of Man\\b|\\bGuam\\b|\\bAndorra\\b|\\bNorthern Mariana Islands\\b|\\bPalau\\b|\\bSeychelles\\b|\\bCuraçao\\b|\\bNetherlands\\b|\\bAntigua and Barbuda\\b|\\bBarbados\\b|\\bHeard Island and McDonald Islands\\b|\\bSaint Vincent and the Grenadines\\b|\\bJan Mayen\\b|\\bGrenada\\b|\\bMalta\\b|\\bSaint Helena, Ascension and Tristan da Cunha\\b|\\bMaldives\\b|\\bCayman Islands\\b|\\bSaint Kitts and Nevis\\b|\\bNiue\\b|\\bAkrotiri and Dhekelia\\b|\\bSaint Pierre and Miquelon\\b|\\bCook Islands\\b|\\bAmerican Samoa\\b|\\bMarshall Islands\\b|\\bAruba\\b|\\bLiechtenstein\\b|\\bBritish Virgin Islands\\b|\\bWallis and Futuna\\b|\\bChristmas Island\\b|\\bJersey\\b|\\bMontserrat\\b|\\bAnguilla\\b|\\bGuernsey\\b|\\bSan Marino\\b|\\bBritish Indian Ocean Territory\\b|\\bSaint Martin\\b|\\bBermuda\\b|\\bBouvet Island\\b|\\bPitcairn Islands\\b|\\bNorfolk Island\\b|\\bSint Maarten\\b|\\bMacau\\b|\\bTuvalu\\b|\\bNauru\\b|\\bSaint Barthélemy\\b|\\bTokelau\\b|\\bGibraltar\\b|\\bWake Island\\b|\\bClipperton Island\\b|\\bNavassa Island\\b|\\bAshmore and Cartier Islands\\b|\\bSpratly Islands\\b|\\bdisputed\\b|\\bCoral Sea Islands\\b|\\bMonaco\\b|\\bVatican City\\b");
	public Pattern Republic = Pattern
			.compile("\\bFederal\\b|\\bFederation\\b|\\bRepublic\\b|\\bStates\\b|\\bFederative\\b|\\bKingdom\\b|\\bUnited\\b|\\bState\\b");
	public Pattern RepublicOfPanama = Pattern.compile("\\bRepublic of Panama\\b");
	public Pattern Nothing = Pattern.compile("\\bSultanate of Oman\\b");

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if (NP.depth() != 2)
			return Rule.Inapplicable;// 如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if (headword.tag().startsWith("NN")) {
			return Rule.Applicable;// 如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if (this.applicable(objects) == Rule.Inapplicable)
			return Rule.Inapplicable;

		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();

		TregexPattern pattern = null;
		TregexMatcher matcher = null;

		/*
		 * 名词短语中含有的冠词，也可能没有
		 */
		sb.clear();
		sb.append("/\\bDT\\b/>,/\\bNP\\b/");
		pattern = TregexPattern.compile(sb.toString());
		matcher = pattern.matcher(NP);

		/*
		 * 名词短语中
		 */
		sb.clear();
		for (TaggedWord tw : NP.taggedYield()) {
			sb.append(tw.word()).append(" ");
		}

		if (this.Country.matcher(sb.toString()).find()) {
			if (this.Republic.matcher(sb.toString()).find()) {
				if ( this.RepublicOfPanama.matcher(sb.toString()).find()) {
					if (matcher.find()) {
						BadEgg egg = new BadEgg();
						egg.whereIsWrong = NP;
						egg.setProperty(BadEgg.Error);
						egg.setViolatedRule(this);
						return egg;
					} else {
						return null;
					}					
				} else {
					if (sb.toString().trim().startsWith("the")
							|| sb.toString().trim().startsWith("The")) {
						return null;
					} else {
						BadEgg egg = new BadEgg();
						egg.whereIsWrong = NP;
						egg.setProperty(BadEgg.Error);
						egg.setViolatedRule(this);
						return egg;
					}					
				}

			} else if (this.Nothing.matcher(sb.toString()).find()){
				if (sb.toString().trim().startsWith("the")
						|| sb.toString().trim().startsWith("The")) {
					return null;
				} else {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;
				}
			} else {
				if (matcher.find()) {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;
				} else {
					return null;
				}					
			}
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.append("<p>");
		sb.append("如果表示国家的短语中含有这些单词：Federal、Federation、Republic、States、Federative、Kingdom、United、State，一般前面要加定冠词the。");
		sb.append("<br>");
		sb.append("如果没有，则不使用冠词。");
		sb.append("<br>");
		sb.append("但也有例外，比如：Republic of Panama就不用冠词，The Sultanate of Oman就需要使用定冠词the。");
		sb.append("</p>");
		return sb.toString();
	}
}
